package com.inspector.app

import android.content.Context
import android.content.Intent
import android.view.Gravity
import androidx.multidex.MultiDex
import com.blankj.utilcode.util.LogUtils
import com.example.commen.base.UCApplication
import com.example.commen.bean.EventBusEntity
import com.example.commen.manager.ActivityManager
import com.example.commen.other.ToastStyle
import com.example.commen.resource.CommentContect
import com.example.commen.utile.CommonUtils
import com.hjq.toast.Toaster
import com.inspector.app.di.network.api.LoginInfo
import com.inspector.app.ui.base.initMenuTree
import com.inspector.app.ui.base.initNet
import com.inspector.app.ui.base.initUserinfo
import com.inspector.app.ui.flight.mapsearch.poi.GDSearchInstance
import com.inspector.app.ui.login.LoginActivity
import com.inspector.app.util.CommenArgs
import com.inspector.app.util.ConstantUtil
import com.inspector.app.util.SharedPreferencesUtils
import dagger.hilt.android.HiltAndroidApp
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@HiltAndroidApp
class BaseApp : UCApplication() {
    companion object {
        private var instance: BaseApp? = null

        @JvmStatic
        fun getInstance(): BaseApp {
            if (instance == null) {
                synchronized(BaseApp::class.java) {
                    if (instance == null) {
                        instance = BaseApp()
                    }
                }
            }
            return instance!!
        }
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }


    override fun onCreate() {
        super.onCreate()
        instance = this // 确保 instance 被初始化
        initialize(applicationContext)

    }

    private fun initialize(applicationContext: Context) {
        EventBus.getDefault().register(this)
        initNet(this)
        SharedPreferencesUtils.init(applicationContext)
        //获取本地个人信息以及权限树
        initUserinfo()
        initMenuTree()
        //弹窗
        Toaster.init(this, ToastStyle())
        Toaster.setDebugMode(BuildConfig.DEBUG)
        Toaster.setGravity(Gravity.CENTER, 0, 100)
        //[高德地图SDK初始化]
        GDSearchInstance.instance.setApiKey(BuildConfig.GaoDeApiKey)
    }

    open fun loginOut() {
        if (CommenArgs.getIsLogin()) {
            CommenArgs.setIsLogin(false)
            CommenArgs.loginInfo = LoginInfo()
            ActivityManager.getInstance().finishAllActivities(LoginActivity::class.java)
            SharedPreferencesUtils.remove(CommentContect.SpConstant.LoginInfo)
            SharedPreferencesUtils.remove(ConstantUtil.PASSWORD)
            SharedPreferencesUtils.remove(CommentContect.SpConstant.UserInfo)
            SharedPreferencesUtils.remove(CommentContect.SpConstant.MenuTree)
            CommonUtils.EventPost(CommentContect.EventBusCode.LoginError)

        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onEventMainThread(event: EventBusEntity) {
        if (event.msgId == CommentContect.EventBusCode.LoginError) {
            loginOut()
            ActivityManager.getInstance().finishAllActivities()
            startActivity(Intent(getCurrentActivity(), LoginActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
            ActivityManager.getInstance().topActivity!!.overridePendingTransition(0, 0)
        }
        LogUtils.i("eventBus:" + "onEventMainThread: BaseActivity")
    }
}